package com.Sdebruijn.curriculumvitae.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Subclass of {@link Experience} representing my work experience
 * 
 * @author Stefan de Bruijn
 */
@DatabaseTable(tableName = "Work")
public class Work extends Experience {

	Work() {
		// all persisted classes must define a no-arg constructor with at least
		// package visibility
	}

	/**
	 * {@link Boolean} indicating if this was an internship
	 */
	@DatabaseField
	@SerializedName("internship")
	// TODO Use this to try out Enums with ORMLite/Gson ?
	public boolean internship;
	
	/**
	 * {@link Boolean} indicating if this was a sidejob
	 */
	@DatabaseField
	@SerializedName("sidejob")
	// TODO Use this to try out Enums with ORMLite/Gson ?
	public boolean sidejob;

	/**
	 * {@link Double} indicating the end result if it was an internship
	 */
	@DatabaseField
	@SerializedName("result")
	// TODO would be better suitable if Internship was an Enum
	public double result;

	/**
	 * {@link String} indicating the link to the HBO-kennisbank page
	 */
	@DatabaseField
	@SerializedName("hboKennisbank")
	// TODO would be better suitable if Internship was an Enum
	public String hboKennisbank;

	/**
	 * {@link Collection} of {@link WorkTask}s
	 */
	/*
	 * Tasks: Needs to be a Collection in order to be a child of
	 * ForeignCollection to work with ORMLite and Gson at the same time
	 */
	@ForeignCollectionField(eager = true)
	@SerializedName("tasks")
	public Collection<WorkTask> tasks;

	/**
	 * Returns Collection tasks as ArrayList for easy iteration
	 * 
	 * @return ArrayList of WorkTask
	 */
	public ArrayList<WorkTask> getTasksAsList() {
		ArrayList<WorkTask> taskList = new ArrayList<WorkTask>();
		Iterator<WorkTask> iterator = tasks.iterator();
		while (iterator.hasNext()) {
			taskList.add((WorkTask) iterator.next());
		}
		return taskList;
	}

	/*
	 * Required methods to have this class as Parcelable Having this class
	 * Parcelable will make it easier to restore it into fragments with
	 * savedInstanceState
	 */

	/*
	 * Just used as id - hashCode() will do fine
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return this.hashCode();
	}

	/*
	 * Serialize my object attributes one by one
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeInt(this.id);
		parcel.writeString(this.end_date);
		parcel.writeString(this.start_date);
		parcel.writeString(this.title);
		parcel.writeByte((byte) (this.current ? 1 : 0));

		parcel.writeString(this.hboKennisbank);
		parcel.writeByte((byte) (this.internship ? 1 : 0));
		parcel.writeDouble(this.result);

		parcel.writeParcelable(this.institution, 0);
	}

	/**
	 * Required for any Parcelable class to be recreated. Will be called to
	 * de-seralize the object.
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public static final Parcelable.Creator<Work> CREATOR = new Parcelable.Creator<Work>() {
		public Work createFromParcel(Parcel source) {
			return new Work(source);
		}

		public Work[] newArray(int size) {
			return new Work[size];
		}
	};

	/**
	 * <b>Do not use!</b> Private constructor for the Parcelable Creator. Will
	 * de-serialize the object.
	 * 
	 * @param source
	 */
	private Work(Parcel source) {
		this.id = source.readInt();
		this.current = source.readByte() == 1;
		this.internship = source.readByte() == 1;
		this.end_date = source.readString();
		this.hboKennisbank = source.readString();
		this.start_date = source.readString();
		this.title = source.readString();
		this.result = source.readDouble();

		this.institution = source.readParcelable(Institution.class.getClassLoader());

	}
}
